//
//  LoginViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/23.
//

#import "LoginViewController.h"
#import "ForgetViewController.h"
#import "RegisterViewController.h"
#import "InformationViewController.h"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHideNavigationBar = YES;
}

- (void)initSubviews {
    [super initSubviews];
    
    UIImageView *backImage = [[UIImageView alloc]initWithImage:UIImageMake(@"profile_back")];
    backImage.frame = CGRectMake(0, 0, kScreenWidth, 220);
    [self.view addSubview:backImage];
    
    MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    root.myTop = 46 + NavigationContentTop;
    root.myHorzMargin = 15;
    root.subviewVSpace = 15;
    [self.view addSubview:root];
    
    UILabel *name = [UILabel createLabelText:@"请登录" font:UIFontBoldMake(24) textColor:COLOR1515];
    name.mySize = kAutoSize;
    [root addSubview:name];
    
    NSArray *textArr = @[@{@"name":@"请输入用户名",@"img":@"login_username"},
                         @{@"name":@"请输入密码",@"img":@"login_pass"}];
    for (int i = 0; i < textArr.count; i++) {
        
        MyLinearLayout *item = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        item.myHorzMargin = 0;
        item.gravity = MyGravity_Vert_Center;
        item.myHeight = 30;
        item.myTop = i==0 ?50:0;
        item.subviewHSpace = 10;
        [root addSubview:item];

        {
            UIImageView *logo = [[UIImageView alloc]initWithImage:UIImageMake(textArr[i][@"img"])];
            logo.mySize = CGSizeMake(20, 20);
            [item addSubview:logo];
            
            QMUITextField * tf = QMUITextField.new;
            tf.weight = 1;
            NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:textArr[i][@"name"] attributes:
                                                         @{NSForegroundColorAttributeName:HexColor(@"#BABABA"),
                                                           NSFontAttributeName:UIFontMake(15)}
            ];
            tf.attributedPlaceholder = attrString;
            tf.myHeight = 30;
            tf.keyboardType = i == 0 ? UIKeyboardTypeNumberPad: UIKeyboardTypeAlphabet;
            tf.maximumTextLength = i ==0 ? 11 : 20;
            tf.secureTextEntry = i == 0 ? NO : YES;
            tf.clearButtonMode = UITextFieldViewModeWhileEditing;
            tf.tag = 100 + i;
            [item addSubview:tf];
        }
        
        UIView *line = UIView.new;
        line.myHorzMargin = 0;
        line.myHeight = 0.5;
        line.backgroundColor = HexColor(@"#E3E3E3");
        [root addSubview:line];
        
    }
    
    UIButton *forgetBtn = [UIButton createButtonText:@"忘记密码" font:UIFontMake(14) textColor:COLOR666];
    forgetBtn.myRight = 0;
    forgetBtn.mySize = kAutoSize;
    [forgetBtn addTarget:self action:@selector(forgetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [root addSubview:forgetBtn];
    
    
    UIButton *loginBtn = [UIButton createButtonText:@"登录" font:UIFontMake(14) textColor:COLOR_4];
    loginBtn.myHorzMargin = 0;
    loginBtn.myHeight = 46;
    ViewCorner(loginBtn, 23);
    [loginBtn az_setGradientBackgroundWithColors:@[HexColor(@"#4BD8B1"),HexColor(@"#2CAED8")] locations:@[@0.0,@1.0] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 1)];
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [root addSubview:loginBtn];
    
    UIButton *registerBtn = [UIButton createButtonText:@"没有账号？立即注册" font:UIFontMake(14) textColor:COLOR999];
    registerBtn.myHorzMargin = 0;
    registerBtn.myHeight = 46;
    ViewCorner(registerBtn, 23);
    ViewBorder(registerBtn, 1.0, HexColor(@"#E8E8E8"));
    [registerBtn addTarget:self action:@selector(registerBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [root addSubview:registerBtn];
    
}

- (void)forgetBtnClick {
    ForgetViewController * vc = ForgetViewController.new;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)loginBtnClick {
    [self.view endEditing:YES];
    
//    QMUITextField * phone = (QMUITextField *)[self.view viewWithTag:100];
//    if(phone.text.length < 11){
//        [QMUITips showError:@"请输入正确的手机号"];
//        return;
//    }
//    QMUITextField * pass = (QMUITextField *)[self.view viewWithTag:101];
//    if(pass.text.length < 6){
//        [QMUITips showError:@"密码最少为6位"];
//        return;
//    }
    
//    NSMutableDictionary *mutDic = [NSMutableDictionary dictionary];
//    mutDic[@"phone"] = phone.text;
//    mutDic[@"pass"] = pass.text;
    
    UserInfo *user = [UserInfo new];
    user.phone = @"15268143457";
    [ZWJCache saveUserInfo:user];
    [_notification postNotificationName:loginSuccess object:nil];
}

- (void)registerBtnClick {
    InformationViewController *vc = InformationViewController.new;
//    RegisterViewController * vc = RegisterViewController.new;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
